<script setup lang="ts">
import { defineProps, defineEmits } from 'vue'

import { NFlex, NDivider, NTag, useThemeVars } from 'naive-ui'

import { boolToStr } from '../utils/FormatUtils'

import { PathUtils } from 'cetools'
import { javaTypeToTsType, ApiMethodParameter, ApiMethod } from '../entities/Entity'

const themeVars = useThemeVars()

interface Props {
    data: ApiMethod
    parentPath?: string
}

const getSimpleType = (str: string) => {
    if (str != null) {
        const data = str.split('.')
        return data[data.length - 1]
    }
    return str
}

const getTagType = (require: boolean) => {
    if (require) return 'error'
    return 'info'
}

const renderPostMethod = (type: string, dataType: string) => {
    if (type === 'POST') {
        return 'POST/' + dataType
    }
    return type
}

const isArrayParameter = () => {
    return props.data.parameters instanceof Array
}

const showLine = () => {
    if (!isArrayParameter()) return true
    const len = (props.data.parameters as Array<ApiMethodParameter>).length
    return isArrayParameter() && len >= 1
}

const emits = defineEmits(['showEntity'])

const emitClickRequestEntity = (className: string) => {
    emits('showEntity', className)
}

const emitClickEntity = (className: string) => {
    const index = props.data.resultGenerics?.indexOf(className)
    let classNameChain = className
    if (index) {
        for (let i = index + 1; i < props.data.resultGenerics!.length; i++) {
            classNameChain += '|' + props.data.resultGenerics![i]
        }
    }
    emits('showEntity', classNameChain)
}

const props = defineProps<Props>()
</script>
<template>
    <div class="it">
        <div class="title" :style="`background-color:${themeVars.actionColor}`">
            <n-flex justify="space-between">
                <span class="text tl fw">函数:{{ props.data.description }}</span>
                <span class="text tl fw"
                    >请求方法:{{
                        renderPostMethod(props.data.requestMethod, props.data.postDataType)
                    }}</span
                >
                <span class="text tl fw"
                    >请求路径:{{
                        PathUtils.comboPath('' + props.parentPath, props.data.path)
                    }}</span
                >
            </n-flex>
        </div>
        <n-divider v-if="showLine()" style="height: 5px; margin: 0px"></n-divider>
        <n-flex v-if="!isArrayParameter()" justify="space-between">
            <span class="text tl fw"
                >请求参数:<n-tag
                    class="ci"
                    :type="getTagType((props.data.parameters as ApiMethodParameter).require)"
                    @click="
                        emitClickRequestEntity((props.data.parameters as ApiMethodParameter).type)
                    "
                    >{{ getSimpleType((props.data.parameters as ApiMethodParameter).type) }}</n-tag
                ></span
            >
            <span class="text tl fw"
                >必要参数:{{
                    boolToStr((props.data.parameters as ApiMethodParameter).require)
                }}</span
            >
            <span class="text tl fw"
                >基本类型:{{
                    boolToStr((props.data.parameters as MethodParameter).isPrimitive)
                }}</span
            >
        </n-flex>
        <n-flex
            v-for="item in props.data.parameters as Array<ApiMethodParameter>"
            :key="item.type"
            justify="space-between"
        >
            <span v-if="isArrayParameter()" class="text tl fw"
                >请求参数:<n-tag
                    class="ci"
                    :type="getTagType(item.require)"
                    @click="emitClickEntity(item.type)"
                    >{{ javaTypeToTsType(item.type) }}</n-tag
                ></span
            >
            <span v-if="isArrayParameter()" class="text tl fw"
                >必要参数:{{ boolToStr(item.require) }}</span
            >
            <span v-if="isArrayParameter()" class="text tl fw"
                >基本类型:{{ boolToStr(item.isPrimitive) }}</span
            >
        </n-flex>
        <n-flex justify="space-between">
            <span>
                <n-tag class="tag">返回值:</n-tag>
                <n-tag class="ci tag" @click="emitClickEntity(props.data.resultType)">{{
                    javaTypeToTsType(props.data.resultType)
                }}</n-tag>
                <n-tag
                    v-for="item in props.data.resultGenerics"
                    :key="item"
                    class="ci tag"
                    @click="emitClickEntity(item)"
                    >{{ javaTypeToTsType(item) }}</n-tag
                >
            </span>
        </n-flex>
    </div>
</template>

<style scoped>
.it {
    color: inherit;
    margin-bottom: 0.5rem;
    border-radius: 6px;
    border: 1px solid rgba(99, 99, 99, 0.2);
    /* background-color: rgba(99, 99, 99, 0.1); */
    padding: 5px 5px 5px 5px;
}
.text {
    color: inherit;
    font-size: inherit;
    font-weight: 300;
    line-height: 14px;
}

.title {
    width: 100%;
    border-radius: 5px;
}

.tag {
    font-size: inherit;
    font-weight: 400;
    line-height: 28px;
    padding-left: 8px;
    padding-right: 8px;
    margin-top: 5px;
    margin-left: 5px;
}

.tl {
    text-align: left;
}

.fw {
    width: 20rem;
}

.tr {
    text-align: right;
}

.ci {
    cursor: pointer;
}
</style>
